'''
dietypes.py - Grognard die types

Copyright 2013 D. Anthony Robinson

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

	http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
'''
__author__ = 'D. Anthony Robinson <d.anthony.robinson@gmail.com>'

import random

from abc import ABCMeta

from grognard.dice.exceptions import DiceException

class ADie(object):
    '''Abstract Base Class for Die classes.
    Contains a Tuple that holds the faces of the die'''
    __metaclass__ = ABCMeta
    faces = tuple()

    def roll(self):
        '''Rolls the die, returning the result. Return type depends on
        subclass' face types.

        Raises: DiceException if unable to randomize return'''
        try:
            return random.choice(self.faces)
        except Exception as ex:
            msg = ex.message
            raise DiceException(msg)


class StandardDie(ADie):
    '''A standard, numbered die. This can be any typical polyhedral die, and
    defaults to a six-sided die (d6), numbered 1-6.
    '''
    def __init__(self, sides=6):
        '''Constructor. Creates a new StandardDie, with sides numbered 1 and up
        consecutively.

        sides: int, number of sides. Default 6

        Returns: StandardDie object, subclass of ADie

        Raises: ValueError if sides is not int
        '''
        if not isinstance(sides, int):
            err_msg = ('StandardDie only takes ints as number of sides,' +
                    ' got {0}'.format(type(sides)))
            raise ValueError(err_msg)
        if sides:
            self.faces = tuple(range(1, sides+1))

